package com.ews.onlineexchange.shiro;

import org.apache.shiro.mgt.RealmSecurityManager;
import org.apache.shiro.web.env.DefaultWebEnvironment;
import org.apache.shiro.web.env.EnvironmentLoaderListener;
import org.apache.shiro.web.env.WebEnvironment;

import javax.inject.Inject;
import javax.servlet.ServletContext;

/**
 * @program: SdewsOnlineExchange
 * @description:
 * @author: wangchangqing
 * @create: 2020-12-29 15:24
 **/
public class CustomEnvironmentLoaderListener extends EnvironmentLoaderListener {

    @Inject
    private OnlineExchangeRealm onlineExchangeRealm;

    @Override
    protected WebEnvironment createEnvironment(ServletContext pServletContext) {
        WebEnvironment environment = super.createEnvironment(pServletContext);
        RealmSecurityManager rsm = (RealmSecurityManager) environment.getSecurityManager();
//        PasswordService passwordService = new DefaultPasswordService();
//        HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(UserService.HASH_ALGORITHM);
//        matcher.setHashIterations(UserService.HASH_INTERATIONS);
//        onlineExchangeRealm.setCredentialsMatcher(matcher);
        rsm.setRealm(onlineExchangeRealm);
        ((DefaultWebEnvironment) environment).setSecurityManager(rsm);
        return environment;
    }

}
